<?

class osGeneric {
	public $id = 'generic';
	public $tabs = array();
	public $device;
	public function __construct(&$device=null) {
		$this->device = $device;
	}
	public $snmpSections = array(
		'HOST-RESOURCES-MIB',
		'IF-MIB',
		'SNMPv2-MIB',
		'TCP-MIB',
		'IP-MIB',
		'UDP-MIB'
	);
	public function getResources() {
	
		// Operating System Information
		
		
		// Device Information
		foreach ($this->device->snmp->find('HOST-RESOURCES-MIB','^hrDeviceIndex') AS $key=>$index) {
			echo $key.'<br>';
			$item = $this->device->snmp->find('HOST-RESOURCES-MIB','hrDevice([a-zA-z0-9]+).'.$index,true);
			$item = $item[$index];
			switch (substr($item['hrDeviceType'],strrpos(':',$item['hrDeviceType']))) {
				case 'hrDeviceProcessor':
					$this->device->cpus[]  = new objProcessor($item);
					break;
				case 'hrDeviceNetwork':
					$this->device->interfaces[] = new objInterface($item);
			}
		
		
		}
		
		// Storage/Memory Information
		foreach ($this->device->snmp->find('HOST-RESOURCES-MIB','hrStorageIndex') AS $key=>$index) {
			$item = $this->device->snmp->find('HOST-RESOURCES-MIB','hrStorage([a-zA-z0-9]+).'.$index,true);
			$item = $item[$index];
			switch (substr($item['hrStorageType'],strrpos(':',$item['hrStorageType']))) {
				case 'hrStorageRam':
				case 'hrStorageVirtualMemory':
					$this->device->memory[$index] = new objMemory($item);
					break;
				case 'hrStorageFixedDisk':
				case 'hrStorageCompactDisc':
					$this->device->storage[$index] = new objStorage($item);
					break;
					
			}
		
		
		}
	
		
	
	
	
	}
	public function getUptime() { return $this->device->snmp->get('HOST-RESOURCES-MIB','hrSystemUptime.0');}
	public function checkDescription($sysDescr='') { }	
	public function getType() { return $this->device->os_type; }
	public function getDescription($len=32) { return $this->device->snmp->get('SNMPv2-MIB','sysDescr.0');}	
	public function getProcessorLoads() {
		echo 'Getting Processor Loads'.chr(10);
		if (isset($this->device->cpus) AND is_array($this->device->cpus)) {
			foreach ($this->device->cpus AS $cpu) {
				$path = $this->device->getRrdPath().'processor_'.$cpu->index.'_load.rrd';
				$load = $this->device->snmp->exists('HOST-RESOURCES-MIB','hrProcessorLoad.'.$cpu->index)?$this->device->snmp->get('HOST-RESOURCES-MIB','hrProcessorLoad.'.$cpu->index):0;
				$cpu->storeUsageRrd($path,$load,$this->device->poll_freq);
			}	
		}
	}
	public function getMemoryUsed() {
		echo 'Getting Memory Used'.chr(10);
		if (isset($this->device->memory) AND is_array($this->device->memory)) {
			foreach ($this->device->memory AS $memory) {
				$path = $this->device->getRrdPath().'storage_'.$memory->index.'.rrd';
				$memory->storeUsageRrd($path,$this->device->poll_freq);
			}	
		}
	}
	public function getProcessorUsage() {	
		$this->device->snmp->getData('.1.3.6.1.4.1.2021.11');
		$path = $this->device->getRrdPath().'processor_usage.rrd';
		if (!is_file($path)) {
		   $opts = array(
				'--step',$this->device->poll_freq,
				'DS:user:COUNTER:600:0:U',
				'DS:system:COUNTER:600:0:U',
				'DS:nice:COUNTER:600:0:U',
				'DS:idle:COUNTER:600:0:U',
				'RRA:AVERAGE:0.5:1:800',
				'RRA:AVERAGE:0.5:6:800',
				'RRA:AVERAGE:0.5:24:800',
				'RRA:AVERAGE:0.5:288:800',
				'RRA:MAX:0.5:1:800',
				'RRA:MAX:0.5:6:800',
				'RRA:MAX:0.5:24:800',
				'RRA:MAX:0.5:288:800'
			);
			if (rrd_create($path,$opts,count($opts)) == 0) {
		   		echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
		   }
		}
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawUser.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawUser.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawSystem.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawSystem.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawNice.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawNice.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawIdle.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawIdle.0'):0;
		echo 'Processor Usage ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}
	
	public function getMemoryUsage() {	
		$this->device->snmp->getData('.1.3.6.1.4.1.2021.4');
		$path = $this->device->getRrdPath().'memory_usage.rrd';
		if (!is_file($path)) {
		   $opts = array(
				'--step',$this->device->poll_freq,
				'DS:totalswap:GAUGE:600:0:10000000000',
				'DS:availswap:GAUGE:600:0:10000000000',
				'DS:totalreal:GAUGE:600:0:10000000000',
				'DS:availreal:GAUGE:600:0:10000000000',
				'DS:totalfree:GAUGE:600:0:10000000000',
				'DS:shared:GAUGE:600:0:10000000000',
				'DS:buffered:GAUGE:600:0:10000000000',
				'DS:cached:GAUGE:600:0:10000000000',
				'RRA:AVERAGE:0.5:1:800',
				'RRA:AVERAGE:0.5:6:800',
				'RRA:AVERAGE:0.5:24:800',
				'RRA:AVERAGE:0.5:288:800',
				'RRA:MAX:0.5:1:800',
				'RRA:MAX:0.5:6:800',
				'RRA:MAX:0.5:24:800',
				'RRA:MAX:0.5:288:800'
			);
			if (rrd_create($path,$opts,count($opts)) == 0) {
		   		echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
		   }
		}
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memTotalSwap.0')?$this->device->snmp->get('UCD-SNMP-MIB','memTotalSwap.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memAvailSwap.0')?$this->device->snmp->get('UCD-SNMP-MIB','memAvailSwap.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memTotalReal.0')?$this->device->snmp->get('UCD-SNMP-MIB','memTotalReal.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memAvailReal.0')?$this->device->snmp->get('UCD-SNMP-MIB','memAvailReal.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memTotalFree.0')?$this->device->snmp->get('UCD-SNMP-MIB','memTotalFree.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memShared.0')?$this->device->snmp->get('UCD-SNMP-MIB::memShared.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memBuffer.0')?$this->device->snmp->get('UCD-SNMP-MIB','memBuffer.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','memCached.0')?$this->device->snmp->get('UCD-SNMP-MIB','memCached.0'):0;
		echo 'Memory Usage ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}
	
	public function getStorageUsage() {	
		foreach ($this->device->storage AS $storage) {	
			$path = $this->device->getRrdPath().'storage_'.$storage->index.'.rrd';
			$storage->storeUsageRrd($path,$this->device->poll_freq);
		}
	}
	
	public function getSystemLoad() {	
		$this->device->snmp->getData('.1.3.6.1.4.1.2021.10');
		$path = $this->device->getRrdPath().'system_load.rrd';
		if (!is_file($path)) {
		   $opts = array(
				'--step',$this->device->poll_freq,
				'DS:1min:GAUGE:600:0:5000',
				'DS:5min:GAUGE:600:0:5000',
				'DS:15min:GAUGE:600:0:5000',
				'RRA:AVERAGE:0.5:1:800',
				'RRA:AVERAGE:0.5:6:800',
				'RRA:AVERAGE:0.5:24:800',
				'RRA:AVERAGE:0.5:288:800',
				'RRA:MAX:0.5:1:800',
				'RRA:MAX:0.5:6:800',
				'RRA:MAX:0.5:24:800',
				'RRA:MAX:0.5:288:800'
			);
			if (rrd_create($path,$opts,count($opts)) == 0) {
		   		echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
		   }
		}
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','laLoadInt.1')?$this->device->snmp->get('UCD-SNMP-MIB','laLoadInt.1'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','laLoadInt.2')?$this->device->snmp->get('UCD-SNMP-MIB','laLoadInt.2'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','laLoadInt.3')?$this->device->snmp->get('UCD-SNMP-MIB','laLoadInt.3'):0;
		echo 'System Load ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}
	
	public function getIPStatistics() {
		$fields = array(
			'InReceives',
			'InHdrErrors',
			'InAddrErrors',
			'InUnknownProtos',
			'InForwDatagrams',
			'ReasmReqds',
			'ReasmOKs',
			'ReasmFails',
			'InDiscards',
			'InDelivers',
			'OutRequests',
			'OutNoRoutes',
			'OutDiscards',
			'OutFragFails',
			'OutFragCreates',
			'OutForwDatagrams'
		);
		// IPv4
		$path = $this->device->getRrdPath().'ip_stats_ipv4.rrd';
		if (!is_file($path)) {
			$opts = array(
				'--step',$this->device->poll_freq,
				'RRA:AVERAGE:0.5:1:600',
				'RRA:AVERAGE:0.5:6:700',
				'RRA:AVERAGE:0.5:24:775',
				'RRA:AVERAGE:0.5:288:797',
				'RRA:MAX:0.5:1:600',
				'RRA:MAX:0.5:6:700',
				'RRA:MAX:0.5:24:775',
				'RRA:MAX:0.5:288:797'
			);
			foreach ($fields AS $field) { $opts[] = 'DS:'.$field.':COUNTER:600:U:100000000000';	}
			if (rrd_create($path,$opts,count($opts)) == 0) {
				echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
			}
		}
		
		
		$ipv4 = $this->device->snmp->find('IP-MIB','ipSystemStats([a-zA-Z0-9]+).ipv4',true);
		$ipv4 = $ipv4['ipv4'];
		$rrd = array();
		$rrd[] = 'N';
		foreach ($fields AS $field) {
			$rrd[] = $this->device->snmp->exists('IP-MIB','ipSystemStats'.$field.'.ipv4')?$this->device->snmp->get('IP-MIB','ipSystemStats'.$field.'.ipv4'):0;
		}
		echo 'IP Statistics IPv4 ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
		}	
		
		
		//IPv6
		$path = $this->device->getRrdPath().'ip_stats_ipv6.rrd';
		if (!is_file($path)) {
			 $opts = array(
				'--step',$this->device->poll_freq,
				'RRA:AVERAGE:0.5:1:600',
				'RRA:AVERAGE:0.5:6:700',
				'RRA:AVERAGE:0.5:24:775',
				'RRA:AVERAGE:0.5:288:797',
				'RRA:MAX:0.5:1:600',
				'RRA:MAX:0.5:6:700',
				'RRA:MAX:0.5:24:775',
				'RRA:MAX:0.5:288:797'
			);
			foreach ($fields AS $field) { $opts[] = 'DS:'.$field.':COUNTER:600:U:100000000000';	}
			if (rrd_create($path,$opts,count($opts)) == 0) {
		   		echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
		   }
		}
		$ipv6 = $this->device->snmp->find('IP-MIB','ipSystemStats([a-zA-Z0-9]+).ipv6');
		$ipv6 = $ipv6['ipv6'];
		$rrd = array();
		$rrd[] = 'N';
		foreach ($fields AS $field) {
			$rrd[] = $this->device->snmp->exists('IP-MIB','ipSystemStats'.$field.'.ipv6')?$this->device->snmp->get('IP-MIB','ipSystemStats'.$field.'.ipv6'):0;
		}
		echo 'IP Statistics IPv6 ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
		}	
	}
	public function getNetStatistics() {
		$protos = array(
			'ip' => array(
				'ipForwDatagrams',
				'ipInDelivers',
				'ipInReceives',
				'ipOutRequests',
				'ipInDiscards',
				'ipOutDiscards',
				'ipOutNoRoutes',
				'ipReasmReqds',
				'ipReasmOKs',
				'ipReasmFails',
				'ipFragOKs',
				'ipFragFails',
				'ipFragCreates',
				'ipInUnknownProtos',
				'ipInHdrErrors',
				'ipInAddrErrors'
			),			
			'snmp' => array (
				'snmpInPkts',
				'snmpOutPkts',
				'snmpInBadVersions',
				'snmpInBadCommunityNames',
				'snmpInBadCommunityUses',
				'snmpInASNParseErrs',
				'snmpInTooBigs',
				'snmpInNoSuchNames',
				'snmpInBadValues',
				'snmpInReadOnlys',
				'snmpInGenErrs',
				'snmpInTotalReqVars',
				'snmpInTotalSetVars',
				'snmpInGetRequests',
				'snmpInGetNexts',
				'snmpInSetRequests',
				'snmpInGetResponses',
				'snmpInTraps',
				'snmpOutTooBigs',
				'snmpOutNoSuchNames',
				'snmpOutBadValues',
				'snmpOutGenErrs',
				'snmpOutGetRequests',
				'snmpOutGetNexts',
				'snmpOutSetRequests',
				'snmpOutGetResponses',
				'snmpOutTraps',
				'snmpSilentDrops',
				'snmpProxyDrops'
			),
			'tcp' => array (
				'tcpActiveOpens',
				'tcpPassiveOpens',
				'tcpAttemptFails',
				'tcpEstabResets',
				'tcpCurrEstab', 
				'tcpInSegs',
				'tcpOutSegs',
				'tcpRetransSegs',
				'tcpInErrs',
				'tcpOutRsts',
				'tcpHCInSegs',
				'tcpHCOutSegs'
			),
			'udp' => array (
				'udpInDatagrams',
				'udpOutDatagrams',
				'udpInErrors',
				'udpNoPorts'
			),
			'icmp' => array(
				'icmpInMsgs',
				'icmpOutMsgs',
				'icmpInErrors',
				'icmpOutErrors',
				'icmpInEchos',
				'icmpOutEchos',
				'icmpInEchoReps',
				'icmpOutEchoReps',
				'icmpInDestUnreachs',
				'icmpOutDestUnreachs',
				'icmpInParmProbs',
				'icmpInTimeExcds',
				'icmpInSrcQuenchs',
				'icmpInRedirects',
				'icmpInTimestamps',
				'icmpInTimestampReps',
				'icmpInAddrMasks',
				'icmpInAddrMaskReps',
				'icmpOutTimeExcds',
				'icmpOutParmProbs',
				'icmpOutSrcQuenchs',
				'icmpOutRedirects',
				'icmpOutTimestamps',
				'icmpOutTimestampReps',
				'icmpOutAddrMasks',
				'icmpOutAddrMaskReps'
			)
		);
		
		foreach ($protos AS $proto=>$fields) {
			$path = $this->device->getRrdPath().'netstats_'.$proto.'.rrd';
			if (!is_file($path)) {
			   $opts = array(
					'--step',$this->device->poll_freq,
					'RRA:AVERAGE:0.5:1:600',
					'RRA:AVERAGE:0.5:6:700',
					'RRA:AVERAGE:0.5:24:775',
					'RRA:AVERAGE:0.5:288:797',
					'RRA:MAX:0.5:1:600',
					'RRA:MAX:0.5:6:700',
					'RRA:MAX:0.5:24:775',
					'RRA:MAX:0.5:288:797'
				);
				foreach ($fields AS $field) { $opts[] = 'DS:'.substr($field,strlen($proto)).':COUNTER:600:U:100000000000';	}
				echo 'Creating '.$path.chr(10);
				if (rrd_create($path,$opts,count($opts)) == 0) {
					echo 'Error: '.rrd_error().chr(10);
					print_r($opts);
					// Log Error
					return false;
			   }
			}
			$rrd = array();
			$rrd[] = 'N';
			$section = '';
			switch ($proto) {
				case 'icmp':
				case 'ip':
					$section = 'IP-MIB';
				break;
				case 'snmp':
					$section = 'SNMPv2-MIB';					
					break;
				case 'tcp':
					$section = 'TCP-MIB';
					break;
				case 'udp':
					$section = 'UDP-MIB';
					break;
				
					
			}
			foreach ($fields AS $field) {
				$rrd[] = $this->device->snmp->exists($section,$field.'.0')?$this->device->snmp->get($section,$field.'.0'):0;
			}
			echo 'Net Statistics '.strtoupper($proto).' ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
			if (rrd_update($path,implode(':',$rrd)) == 0) {
				echo rrd_error();
			}
		}
	}
	public function setProcessesUsersUptimeRrd() {
		// RRD Data File
		$path = $this->device->getRrdPath().'processes_user_uptime.rrd';
		// Up data from SNMP Walk
		$processes = $this->device->snmp->get('HOST-RESOURCES-MIB','hrSystemProcesses.0');
		$users = $this->device->snmp->get('HOST-RESOURCES-MIB','hrSystemNumUsers.0');
		$uptime = $this->device->snmp->get('HOST-RESOURCES-MIB','hrSystemUptime.0');

		if (!is_file($path)) {
			$opts = array(
				'--step',$this->device->poll_freq,
				'DS:users:GAUGE:600:0:U',
				'DS:procs:GAUGE:600:0:U',
				'DS:uptime:GAUGE:600:0:U',
				'RRA:AVERAGE:0.5:1:800',
				'RRA:AVERAGE:0.5:6:800',
				'RRA:AVERAGE:0.5:24:800',
				'RRA:AVERAGE:0.5:288:800',
				'RRA:MAX:0.5:1:800',
				'RRA:MAX:0.5:6:800',
				'RRA:MAX:0.5:24:800',
				'RRA:MAX:0.5:288:800'
			);
			if (rrd_create($path, $opts, count($opts)) == 0) { 
				echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
			}
		}		
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $users;
		$rrd[] = $processes;
		$rrd[] = $uptime;
		echo 'Processes Users Uptime ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}
}